package br.hm.telephoneoperator.impl;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.protocol.HttpContext;
import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

public class RequestSearchPortability implements HTMLRequest {

	private static final String URL_LOAD_SEARCH_PORTABILITY = "/consultanumero/consulta/consultaHistoricoRecente!carregar.action";	

	private static final String TAG_CAPTCHA_XPATH = "//img[@id='jcap']";
	
	
	private static final String TAG_JCID_XPATH = "//input[@name='jcid']";
	
	
	private HttpClient httpClient;

	private HttpContext localContext;

	@Override
	public void submit(HTMLLocalContext htmlLocalContext)
			throws HTMLRequestException {

		HttpGet httpGet = new HttpGet(URL_HOST + URL_LOAD_SEARCH_PORTABILITY);
		HttpEntity entity = null;
		Reader readResponse = null;

		try {
			HttpResponse httpResponse = httpClient.execute(httpGet,
					localContext);
			if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK
					|| httpResponse.getEntity() == null) {
				throw new HTMLRequestException(
						"Status Code response invalid = "
								+ httpResponse.getStatusLine().getStatusCode());
			}
			try {
				entity = httpResponse.getEntity();
				// Falta configurar o Charset
				readResponse = new InputStreamReader(entity.getContent());

				HtmlCleaner cleaner = new HtmlCleaner();
				CleanerProperties props = cleaner.getProperties();
				props.setAllowHtmlInsideAttributes(true);
				props.setAllowMultiWordAttributes(true);
				props.setRecognizeUnicodeChars(true);
				props.setOmitComments(true);

				
				TagNode node = cleaner.clean(readResponse);
				
				Object[] jcap_nodes = node.evaluateXPath(TAG_CAPTCHA_XPATH);
				Object[] jcid_nodes = node.evaluateXPath(TAG_JCID_XPATH);
				
				if (jcap_nodes.length > 0){
					TagNode jcap_node = (TagNode)  jcap_nodes[0];
					String urlCaptcha = jcap_node.getAttributeByName("src");
					htmlLocalContext.setParameter(
							PARAM_SEARCH_PORTABILITY_URL_CAPTCHA, urlCaptcha);
					
				}else{
					throw new HTMLRequestException("Throwable get url catcha");
				}
				
				if (jcid_nodes.length > 0){
					TagNode jcid_node = (TagNode)  jcid_nodes[0];
					String value_jcid = jcid_node.getAttributeByName("value");
					htmlLocalContext.setParameter(
							PARAM_SEARCH_PORTABILITY_JCID, value_jcid);
					
				}else{
					
					throw new HTMLRequestException("Throwable get value jcid");
				}
				
			} catch (XPatherException e) {
				throw new HTMLRequestException("Throwable parse HTML -"
						+ e.getMessage(), e);

			} finally {
				if (entity != null) {
					entity.consumeContent();
				}
				if (readResponse != null) {
					readResponse.close();
				}
			}

		} catch (ClientProtocolException e) {
			throw new HTMLRequestException("Throwable connection -"
					+ e.getMessage(), e);
		} catch (IOException e) {
			throw new HTMLRequestException("Throwable protocol HTTP -"
					+ e.getMessage(), e);

		}
	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	public HttpContext getLocalContext() {
		return localContext;
	}

	public void setLocalContext(HttpContext localContext) {
		this.localContext = localContext;
	}

}
